#!/usr/bin/env python

from action import FrameStack
from action import Action
from actor import Actor, KilledActor
from player import Player
from renderer import Renderer
import common
import copy
import pygame

def main():
    # Initialize Everything
    pygame.init()
    screen = pygame.display.set_mode((1024, 768))
    pygame.display.set_caption('FSTV Renderer engine test')
    pygame.mouse.set_visible(1)

    # Prepare Game Objects
    clock = pygame.time.Clock()
    render = Renderer(screen)

    # Prepare Scene
    render.set_background(common.load_image('background.jpg'))

    meca = Player(screen,
                  Action(FrameStack('meca_head_left_')),
                  Action(FrameStack('meca_weels_left_')),
                  (0, 10), (450, 350))
    meca.add_top_action(Action(FrameStack('meca_head_leftdown_')))
    meca.add_top_action(Action(FrameStack('meca_head_down_')))
    meca.add_top_action(Action(FrameStack('meca_head_rightdown_')))
    meca.add_top_action(Action(FrameStack('meca_head_right_')))
    meca.add_top_action(Action(FrameStack('meca_head_rightup_')))
    meca.add_top_action(Action(FrameStack('meca_head_up_')))
    meca.add_top_action(Action(FrameStack('meca_head_leftup_')))
    meca.add_bottom_action(Action(FrameStack('meca_weels_rightup_')))
    meca.add_bottom_action(Action(FrameStack('meca_weels_down_')))
    meca.add_bottom_action(Action(FrameStack('meca_weels_rightdown_')))
    meca.add_bottom_action(Action(FrameStack('meca_weels_right_')))
    meca.add_bottom_action(Action(FrameStack('meca_weels_leftdown_')))
    meca.add_bottom_action(Action(FrameStack('meca_weels_up_')))
    meca.add_bottom_action(Action(FrameStack('meca_weels_leftup_')))
    weel_action = 0
    head_action = 0
    render.add_players('meca1', meca)

    # KilledActor
    explosion_anim = FrameStack('xplo_top_up_')

    # Main Loop
    while 1:
        clock.tick(20)

        # Handle Input Events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                return
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                weel_action = (weel_action + 1) % len(meca.bot_actions)
                meca.set_bottom_action(weel_action)
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_a:
                head_action = (head_action + 1) % len(meca.top_actions)
                meca.set_top_action(head_action)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    render.add_killed_actor(KilledActor(screen,
                                                        Action(explosion_anim),
                                                        event.pos))
        # Draw scene
        render.update()
        pygame.display.flip()


if __name__ == '__main__':
    main()
